home *** CD-ROM | disk | FTP | other *** search
- Imports System.Web
- Imports System.Web.SessionState
-
- ' set the following constant to True to enable persistent custom Sessions
- ' based on XML files. Read comments below for additional steps you
- ' may need to enable xml sessions.
- #Const XML_SESSIONS = False
-
- ' set the following constant to True to enable a custom error page
- #Const CUSTOM_ERROR_PAGE = False
-
- ' set the following constant to True to enable a global output filter
- ' that converts all <B> tags into <STRONG>
- #Const GLOBAL_FILTER = False
-
- Public Class Global
- Inherits System.Web.HttpApplication
-
- #Region " Component Designer Generated Code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Component Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Required by the Component Designer
- Private components As System.ComponentModel.IContainer
-
- 'NOTE: The following procedure is required by the Component Designer
- 'It can be modified using the Component Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- components = New System.ComponentModel.Container()
- End Sub
-
- #End Region
-
- ' these are globally shared variables
- Public Shared StartedTime As Date = Date.Now
- Public Shared Counter As Integer
-
- ' this event fires when the application starts
-
- Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
- Diagnostics.Debug.WriteLine("Application_Start")
- ' Cache the XML file when the application start.
- CacheEmployeesData()
- End Sub
-
- ' this event fires when the application ends
-
- Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
- Diagnostics.Debug.WriteLine("Application_End")
- End Sub
-
- ' Another way to specify the Start event (commented out)
-
- 'Sub Application_OnStart(ByVal sender As Object, ByVal e As EventArgs)
- ' Diagnostics.Debug.WriteLine("Application_OnStart")
- 'End Sub
-
- ' this event fires when the session is started
-
- Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
- Diagnostics.Debug.WriteLine("Session_Start")
- End Sub
-
- ' this event fires when the session ends
-
- Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
- Diagnostics.Debug.WriteLine("Session_End")
- End Sub
-
- ' Alternative ways to specify session events (commented)
-
- 'Sub Session_OnStart(ByVal sender As Object, ByVal e As EventArgs)
- ' Diagnostics.Debug.WriteLine("Session_OnStart")
- 'End Sub
-
- ' this event fires after ASP.NET has created the Session collection
-
- Private Sub Global_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.BeginRequest
- Diagnostics.Debug.WriteLine("Global_BeginRequest")
- End Sub
-
- #If XML_SESSIONS Then
- ' IMPORTANT: create a directory on the following path, or change
- ' the path to match an existing directory
- Const SESSIONDATAPATH = "C:\SessionData\"
-
- Private Sub Global_AcquireRequestState(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.AcquireRequestState
- Diagnostics.Debug.WriteLine("Global_AcquireRequestState")
-
- Dim fs As System.IO.FileStream
- Dim sf As New System.Runtime.Serialization.Formatters.Soap.SoapFormatter()
-
- Try
- ' Get the special cookie, exit if not found.
- Dim cookie As HttpCookie = Request.Cookies("PermSessionID")
- If (cookie Is Nothing) Then
- ' If not found, generate it now (using pseudo-random SessionID).
- cookie = New HttpCookie("PermSessionID", Session.SessionID)
- ' Send it to the client browser, with one-week expiration
- cookie.Expires = Now.AddDays(7)
- ' Uncomment next line to see behavior with much shorter expiration.
- 'cookie.Expires = Now.AddMinutes(1)
- Response.Cookies.Add(cookie)
- ' nothing else to do for now
- Exit Try
- End If
-
- ' The filename is equal to the value of this cookie.
- Dim permSessionId As String = cookie.Value
- ' Build the name of the data file.
- Dim filename As String = SESSIONDATAPATH & permSessionId.ToString & ".xml"
-
- ' Open the file, exit if error
- fs = New System.IO.FileStream(filename, IO.FileMode.Open)
- ' Deserialize the hashtable that contains values.
- Dim ht As Hashtable = DirectCast(sf.Deserialize(fs), Hashtable)
-
- ' Move data into the Session collection.
- Dim key As String
- Session.Clear()
- For Each key In ht.Keys
- Session(key) = ht(key)
- Next
- Catch ex As Exception
- ' ignore any exceptions.
- Finally
- ' Close the stream on exit.
- If Not (fs Is Nothing) Then fs.Close()
- End Try
- End Sub
-
- ' this event fires after ASP.NET is about to save the Session collection
-
- Private Sub Global_ReleaseRequestState(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ReleaseRequestState
- Diagnostics.Debug.WriteLine("Global_ReleaseRequestState")
-
- ' Get the special cookie.
- Dim cookie As HttpCookie = Request.Cookies("PermSessionID")
- ' The value of the cookie is the name of the .ses file.
- Dim permSessionID As String = cookie.Value
-
- ' Move data from the Session collection into a Hashtable.
- Dim ht As New Hashtable(Session.Count)
- Dim key As String
- For Each key In Session.Keys
- ht(key) = Session(key)
- Next
- ' Clear the regular session collection, to save memory.
- Session.Clear()
-
- Dim fs As System.IO.FileStream
- Try
- ' Build the name of the data file.
- Dim filename As String = SESSIONDATAPATH & permSessionID.ToString & ".xml"
- ' Open the file for outout, exit if error
- fs = New System.IO.FileStream(filename, IO.FileMode.Create)
- ' Serialize the hashtable that contains values.
- Dim sf As New System.Runtime.Serialization.Formatters.Soap.SoapFormatter()
- sf.Serialize(fs, ht)
- Catch ex As Exception
- ' ignore any exceptions.
- Finally
- ' Close the stream on exit.
- If Not (fs Is Nothing) Then fs.Close()
- End Try
- End Sub
- #End If
-
- ' other application events, more or less in the order they fire
-
- Private Sub Global_AuthenticateRequest(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.AuthenticateRequest
- Diagnostics.Debug.WriteLine("Global_AuthenticateRequest")
- End Sub
-
- Private Sub Global_AuthorizeRequest(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.AuthorizeRequest
- Diagnostics.Debug.WriteLine("Global_AuthorizeRequest")
- End Sub
-
- Private Sub Global_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Disposed
- Diagnostics.Debug.WriteLine("Global_Disposed")
- End Sub
-
- Private Sub Global_EndRequest(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.EndRequest
- Diagnostics.Debug.WriteLine("Global_EndRequest")
- End Sub
-
- ' this event fires when an unhandled event occurs
-
- Private Sub Global_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Error
- Diagnostics.Debug.WriteLine("Global_Error")
-
- #If CUSTOM_ERROR_PAGE Then
- ' Prepare an error report.
- Response.Clear()
- Response.Write("<H1>An exception has occurred:</H1>")
- ' Display information on the page being processed.
- Response.Write("<b>URL = </b>" & Request.Path & "<br />")
- Response.Write("<b>QueryString = </b>" & Request.QueryString.ToString & "<p>")
- Response.Write("<b>Error details</b><p>")
-
- ' Get a reference to the (real) error that occurred.
- Dim ex As Exception = Server.GetLastError.InnerException
- ' Convert the string in a format that is suitable for HTML output
- Dim errMsg As String = Server.HtmlEncode(ex.ToString)
- errMsg = errMsg.Replace(ControlChars.CrLf, "<BR />")
-
- Response.Write(errMsg)
- Response.End()
- #End If
-
- End Sub
-
- Private Sub Global_PostRequestHandlerExecute(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PostRequestHandlerExecute
- Diagnostics.Debug.WriteLine("Global_PostRequestHandlerExecute")
- End Sub
-
- ' this event fires just before the page executes
-
- Private Sub Global_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRequestHandlerExecute
- Diagnostics.Debug.WriteLine("Global_PreRequestHandlerExecute")
-
- #If GLOBAL_FILTER Then
- ' if we want to filter the page output we assign a filter object
- ' to the Response.Filter property
- ' NOTE: for simplicity, this program relies on the garbage collection
- ' for disposing the ConvertTagFilter. In a more robust implementation
- ' you should destroy it orderly after the HTML has been sent to the client
- Response.Filter = New ConvertTagFilter(Response.Filter)
- #End If
- End Sub
-
- ' other application events
-
- Private Sub Global_PreSendRequestContent(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreSendRequestContent
- Diagnostics.Debug.WriteLine("Global_PreSendRequestContent")
- End Sub
-
- Private Sub Global_PreSendRequestHeaders(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreSendRequestHeaders
- Diagnostics.Debug.WriteLine("Global_PreSendRequestHeaders")
- End Sub
-
- Private Sub Global_ResolveRequestCache(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ResolveRequestCache
- Diagnostics.Debug.WriteLine("Global_ResolveRequestCache")
- End Sub
-
- Private Sub Global_UpdateRequestCache(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.UpdateRequestCache
- Diagnostics.Debug.WriteLine("Global_UpdateRequestCache")
- End Sub
- End Class
-
- ' this module contains a shared variable that is visible from
- ' any other page of the application
-
- Module Globals
- Public Counter As Integer
- End Module